﻿using System.Drawing.Text;
using Boo.Lang;
using System.IO;
using TextConverter;
using UnityEngine;
using UnityEngine.UI;
using System;

public class TextImgController : MonoBehaviour
{
    /// <summary>
    /// 字体列表
    /// </summary>
    public DropDownExtent FontDropdown;
    /// <summary>
    /// 字号列表
    /// </summary>
    public DropDownExtent FontSizeDropdown;
    /// <summary>
    /// 文字输入框
    /// </summary>
    public InputField TextInput;
    /// <summary>
    /// 应用按钮
    /// </summary>
    public Button ApplyBtn;
    /// <summary>
    /// 是否竖排
    /// </summary>
    public Toggle IsVirtical;
    /// <summary>
    /// 生成图片
    /// </summary>
    public RawImage Img;

    /// <summary>
    /// 选择的字体
    /// </summary>
    private Font selectFont;
    /// <summary>
    /// 字体列表
    /// </summary>
    private List<Font> fontList = new List<Font>();

    private Vector2 ImgSize;
    private Converter textureConvert;
    private string fontsPath;

    void Start ()
	{
	    this.textureConvert = new GameObject("TextConvert").AddComponent<Converter>();
	    this.fontsPath = "C:/Windows/Fonts";
	    this.LoadFontsFromPath();
	    this.LoadFontSizeDropDown();
	    this.ApplyBtn.onClick.AddListener(this.ApplyBtnClick);
	    this.FontDropdown.onValueChanged.AddListener(this.GetSelectedFont);
	    this.FontSizeDropdown.onValueChanged.AddListener(this.SetFontSize);

        this.ImgSize = this.Img.GetComponent<RectTransform>().sizeDelta;
	}

    private void LoadFontSizeDropDown()
    {
        this.FontSizeDropdown.options.Clear();
        Dropdown.OptionData itemData;
        for (int i = 16; i <= 300; i++)
        {
            itemData = new Dropdown.OptionData();
            itemData.text = i.ToString();
            this.FontSizeDropdown.options.Add(itemData);
        }

        this.FontSizeDropdown.value = 50;
        this.FontSizeDropdown.captionText.text = this.FontSizeDropdown.options[50].text;
    }

    private void GetSelectedFont(int arg0)
    {
        this.selectFont = this.fontList[arg0];
        this.ApplyBtnClick();
    }

    /// <summary>
    /// 字号设置
    /// </summary>
    /// <param name="arg0"></param>
    private void SetFontSize(int arg0)
    {
        this.ApplyBtnClick();
    }

    /// <summary>
    /// 应用按钮响应
    /// </summary>
    private void ApplyBtnClick()
    {
        if (!string.IsNullOrEmpty(this.TextInput.text))
        {
            this.Img.texture = this.textureConvert.GenerateTexture(this.TextInput.text, this.selectFont,
                int.Parse(this.FontSizeDropdown.captionText.text), this.ImgSize, this.IsVirtical.isOn);
            DrawRectangleMng.Instance.InitStickImg(this.Img.texture);
        }
    }

    /// <summary>
    /// Load 系统字体
    /// </summary>
    private void LoadFontsFromPath()
    {
        if (this.FontDropdown)
        {
            this.FontDropdown.options.Clear();
            this.fontList.Clear();

            DirectoryInfo directoryInfo = new DirectoryInfo(this.fontsPath);
            FileInfo[] fileInfos = directoryInfo.GetFiles("*", SearchOption.AllDirectories);
            List<string> names = new List<string>();

            Dropdown.OptionData itemData;
            for (int i = 0; i < fileInfos.Length; ++i)
            {
                if (fileInfos[i].Name.EndsWith(".TTF") || fileInfos[i].Name.EndsWith(".ttf") || fileInfos[i].Name.EndsWith(".ttc"))
                {
                    PrivateFontCollection fontCollectionpfc = new PrivateFontCollection();
                    string filePath = this.fontsPath + "/" + fileInfos[i].Name;
                    fontCollectionpfc.AddFontFile(filePath);
                    if (fontCollectionpfc.Families.Length == 0)
                    {
                        continue;
                    }

                    Font font = new Font();
                    font = Font.CreateDynamicFontFromOSFont(fontCollectionpfc.Families[0].Name, 16);

                    if (!names.Contains(font.fontNames[0]))
                    {
                        itemData = new Dropdown.OptionData();
                        itemData.text = font.fontNames[0];
                        this.FontDropdown.options.Add(itemData);

                        names.Add(font.fontNames[0]);

                        this.fontList.Add(font);
                    }
                }
            }

            if (this.fontList.Count > 0)
            {
                this.FontDropdown.captionText.text = this.fontList[0].name;
                this.selectFont = this.fontList[0];
            }
        }
    }

}
